package com.chenyun.system.event;

import com.chenyun.common.vo.SessionUser;
import com.chenyun.system.service.ISysLogininforService;
import com.chenyun.tenant.TenantContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author Lonni
 * @date 2022-08-02 20:26
 */
@Component
@Slf4j
public class SysApplicationEventListener {

    @Autowired
    private ISysLogininforService logininforService;
    /**
     * 监听配置文件跟新事件
     * 加载文件存储和sms存储
     *
     * @param configEvent
     */
    @EventListener
    public void updateConfigEvent(SysConfigEvent configEvent) {
        configEvent.updateConfig();
    }

    /**
     * 登录成功事件
     *
     * @param loginEvent
     */
    @EventListener
    public void loginSuccessEvent(LoginEvent loginEvent) {
        SessionUser sessionUser = loginEvent.getSessionUser();
        log.info("用户登录{}成功,所属租户为:{}", sessionUser.getUserId(), sessionUser.getClientId());
        //设置租户id
        TenantContext.me.setTenantId(sessionUser.getClientId());
        //写入登录日志
        logininforService.insertLogininfor(loginEvent);

    }


}
